import WhatIsTheWeather
# import Handler
import json
import asyncio
from pyppeteer import launch
from loguru import logger

import __init__
import os

async def login():
    path = "http://localhost:63342/weather-forecast/WeatherReport.html"
    launch_kwargs = {
        # 默认无头模式
        "headless": False,
    }
    # 启动浏览器
    browser = await launch(launch_kwargs)
    # 打开标签页
    page = await browser.newPage()
    # 输入网址并打开
    await page.goto(path)
    # 倒计时10秒
    await asyncio.sleep(60)

    # 关闭网页
    await page.close()
    # 关闭浏览器
    await browser.close()
    return


def GetInformation():
    logger.debug("GetInformation =>")

    if __init__.DEV_ENV:
        # DEV 开发环境
        #TODO: 读取目录下所有txt文件名称列表

        # string  \u4e2d\u56fd
        loadedInformation = __init__.LoadFile("WeatherInfo.txt")

        # string => dict   中国
        stringInformation = json.loads(loadedInformation)

    else:
        # PROD 生产环境
        bytesInformation = WhatIsTheWeather.GetIt()
        # bytes => dict   中国
        stringInformation = eval(str(bytesInformation, encoding="utf-8"))

        # write down as file for DEV
        __init__.WriteFile(json.dumps(stringInformation), "WeatherInfo.txt")

    logger.info(stringInformation)
    logger.debug("GetInformation <=")
    return stringInformation


def main():
    # dict
    bytesInformation = GetInformation()
    print(type(bytesInformation))
    print(bytesInformation)
    # DICTINFO = Handler.handle(bytesInformation)

    # print("===========Got DICTINFO==================")
    #
    # HtmlGenerator.Generate(DICTINFO)
    # print("===========Generated HTML==================")
    #
    # asyncio.get_event_loop().run_until_complete(login())


if __name__ == '__main__':
    main()
